package com.Adapters.Items;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.Adapters.FollowersAdapter;
import com.Animations.AnimUtil;
import com.Animations.AnimUtil.AnimationCompleted;
import com.Data.Follow;
import com.Data.Follow.onGetFollowCompleted;
import com.Data.User;
import com.Downloaders.ProfilePictureDownloader;
import com.Downloaders.ProfilePictureDownloader.onProfilePictureDownloaded;
import com.Listeners.FollowButtonListener.AddFollowerListener;
import com.Listeners.FollowButtonListener.DeleteFollowerListener;
import com.Managers.FragmentManagerClass;
import com.Managers.UserManagerClass;
import com.TextFormat.UtilFonts;
import com.activities.ActivityProfileUser;
import com.shareup.R;


public class ItemFollowers implements ItemPost {

    private FollowersAdapter adapter;

    private Context context;

    private ViewHolder holder;

    private LayoutInflater inflater;

    private User info;

    private int position;

    public class ViewHolder {

        public ImageView follow;

        public TextView name;

        public ImageView profile;

        public ProgressBar progress;
    }

    class ViewClickListener implements OnClickListener {

        int position;

        public ViewClickListener(int pos) {

            this.position = pos;
        }

        @Override
        public void onClick(View v) {

            {
                showProfilePopUp(position);
            }
        }
    }

    public ItemFollowers(LayoutInflater inflater, User Info,
                FollowersAdapter adapter) {

        this.info = Info;
        this.adapter = adapter;
        this.inflater = inflater;
        this.context = inflater.getContext();

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateIn()
     */
    @Override
    public void AnimateIn() {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateOut(com.Animations.AnimUtil.
     * AnimationCompleted)
     */
    @Override
    public void AnimateOut(AnimationCompleted callback) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#clearAnimatonList()
     */
    @Override
    public void clearAnimatonList() {

        // TODO Auto-generated method stub

    }

    @Override
    public void delete() {

        adapter.remove(info);
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getCommentsCount()
     */
    @Override
    public int getCommentsCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getLikesCount()
     */
    @Override
    public int getLikesCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPostId()
     */
    @Override
    public int getPostId() {

        return info.getID();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPublicationDate()
     */
    @Override
    public String getPublicationDate() {

        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getView(android.view.View)
     */
    @Override
    public View getView(View v, int position) {

        if (v == null) {
            //
            holder = new ViewHolder();

            int resource = R.layout.item_follower;

            v = inflater.inflate(resource, null);
            holder.name = (TextView) v.findViewById(R.id.f_text_name);
            //
            holder.profile = (ImageView) v.findViewById(R.id.aprofile_edit_image);
            holder.follow = (ImageView) v.findViewById(R.id.followers_follow_button);
            holder.progress = (ProgressBar) v.findViewById(R.id.loading_progress);

            UtilFonts.setTextBoldFont(holder.name);

            v.setTag(holder);

        }
        else {
            //
            holder = (ViewHolder) v.getTag();
        }

        setInfo();
        //
        return v;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getViewType()
     */
    @Override
    public int getViewType() {

        return RowType.FOLLOWER.ordinal();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setCommentsCount(int)
     */
    @Override
    public void setCommentsCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setLikesCount(int)
     */
    @Override
    public void setLikesCount(int count) {

        // TODO Auto-generated method stub

    }

    public void showProfilePopUp(int position) {

        Intent intent = new Intent(context, ActivityProfileUser.class);
        intent.putExtra("imgprofile", info.getProfilePicture());
        intent.putExtra("nameprofile", info.getRealName());
        intent.putExtra("IDUSER", info.getID());

        context.startActivity(intent);
        AnimUtil.getActivityTransition(FragmentManagerClass.aMain, true);
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#updatePublicationDate(java.lang.String)
     */
    @Override
    public void updatePublicationDate(String newDate) {

        // TODO Auto-generated method stub

    }

    private void downloadPicture() {

        ProfilePictureDownloader pro = new ProfilePictureDownloader();

        pro.setCallbackProfilePicture(new onProfilePictureDownloaded() {

            @Override
            public void completed(Bitmap result) {

                AnimUtil.AnimateFadeOutIn(null, true, null, holder.progress);
                holder.profile.setImageBitmap(result);

            }
        });
        pro.execute(holder.profile.getTag().toString());
    }

    private void setFollowersListener(Boolean result) {

        if (result) {

            AnimUtil.animateFadeIn(holder.follow);
            holder.follow.setOnClickListener(new DeleteFollowerListener(info.getID(), holder.follow));
        }
        else
            if (info.getPrivacy() == 0) {

                holder.follow.setVisibility(View.GONE);
                holder.follow.setOnClickListener(new AddFollowerListener(info.getID(), holder.follow));
            }
            else {

                holder.follow.setOnClickListener(new AddFollowerListener(info.getID(), holder.follow));
            }

    }

    private void setFollowState() {

        Follow fl = new Follow(info.getID());
        fl.setCallbackGetFollow(new onGetFollowCompleted() {

            @Override
            public void Completed(Boolean result) {

                AnimUtil.animateFadeIn(holder.follow);
                setFollowersListener(result);

            }
        });
        fl.execute(Follow.ACTION_CHECK);

    }

    private void setInfo() {

        holder.name.setText(info.getRealName());
        holder.profile.setTag(info.getProfilePicture());

        // AVOID TO REUSE IT
        holder.name.setOnClickListener(new ViewClickListener(position));
        holder.profile.setOnClickListener(new ViewClickListener(position));

        if (info.getID() != UserManagerClass.UserLogged.getID())
            setFollowState();

        downloadPicture();

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPosition()
     */
    @Override
    public int getPosition() {

        // TODO Auto-generated method stub
        return 0;
    }

}
